using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Beetle;

public class ByteArraySegment
{
	private int a;

	private Stream b;

	internal byte[] c;

	public int Offset;

	public int Count;

	public byte[] Array
	{
		get
		{
			return c;
		}
		private set
		{
			c = value;
		}
	}

	public int BufferLength
	{
		get
		{
			if (Array == null)
			{
				return 0;
			}
			return Array.Length;
		}
	}

	public ByteArraySegment()
	{
	}

	public byte GetData(int index)
	{
		return c[Offset + index];
	}

	public ByteArraySegment(int length)
	{
		Array = new byte[length];
	}

	public void Encoding(string value, Encoding coding)
	{
		try
		{
			if (string.IsNullOrEmpty(value))
			{
				SetInfo(0, 0);
				return;
			}
			int bytes = coding.GetBytes(value, 0, value.Length, Array, 0);
			SetInfo(0, bytes);
		}
		catch (Exception innerexception)
		{
			throw NetTcpException.StringEncodingError(innerexception);
		}
	}

	public void Import(ByteArraySegment e)
	{
		Import(e.Array, e.Offset, e.Count);
	}

	public void Import(Stream steram)
	{
		int count = steram.Read(Array, 0, (int)steram.Length);
		SetInfo(0, count);
		SetPostion(0);
	}

	public void Import(byte[] data, int offet, int count)
	{
		Buffer.BlockCopy(data, offet, Array, 0, count);
		SetInfo(0, count);
		SetPostion(0);
	}

	public string Decoding(Encoding coding)
	{
		if (Count == 0)
		{
			return null;
		}
		return coding.GetString(c, Offset, Count);
	}

	public string Decoding(Encoding coding, byte[] data, int poffset, int pcount)
	{
		return coding.GetString(data, poffset, pcount);
	}

	public string ToBase64String(byte[] data, int poffset, int pcount)
	{
		return Convert.ToBase64String(data, poffset, pcount);
	}

	public string ToBase64String()
	{
		return Convert.ToBase64String(c, Offset, Count);
	}

	public void FromBase64String(string value)
	{
		try
		{
			byte[] array = Convert.FromBase64String(value);
			Import(array, 0, array.Length);
		}
		catch (Exception innerexception)
		{
			throw NetTcpException.StringEncodingError(innerexception);
		}
	}

	public Stream GetStream()
	{
		if (b == null)
		{
			b = new ArraySegmentStream(c);
		}
		return b;
	}

	public void SetInfo(int offset, int count)
	{
		Offset = offset;
		Count = count;
	}

	public void SetPostion(int value)
	{
		a = value;
	}

	public void SetInfo(byte[] data, int offset, int count)
	{
		Array = data;
		Offset = offset;
		Count = count;
	}

	public void Clear()
	{
		Array = null;
	}

	public void EncryptTo(ByteArraySegment segment, DESCryptoServiceProvider mDESProvider)
	{
		MemoryStream memoryStream = new MemoryStream(segment.c);
		using CryptoStream cryptoStream = new CryptoStream(memoryStream, mDESProvider.CreateEncryptor(), CryptoStreamMode.Write);
		cryptoStream.Write(c, Offset, Count);
		cryptoStream.FlushFinalBlock();
		segment.SetInfo(0, (int)memoryStream.Position);
		cryptoStream.Close();
	}

	public void DecryptTo(ByteArraySegment segment, DESCryptoServiceProvider mDESProvider)
	{
		MemoryStream stream = new MemoryStream(c, Offset, Count);
		int num = 0;
		using CryptoStream cryptoStream = new CryptoStream(stream, mDESProvider.CreateDecryptor(), CryptoStreamMode.Read);
		for (int num2 = (byte)cryptoStream.ReadByte(); num2 >= 0; num2 = cryptoStream.ReadByte())
		{
			segment.c[num] = (byte)num2;
			num++;
		}
		segment.SetInfo(0, num);
	}
}
